package com.ghck.message.config;

import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.ghck.message.util.ConfigUtil;
import com.ghck.message.util.MyErrorHandling;
import com.ghck.message.util.PushUtil;

@Configuration
public class ResourceConfig {
	@Configuration
	@Profile(UserProfile.INTG)
	@PropertySource(value = {
							"classpath:/intg/rpc.properties",
							"classpath:/intg/jdbc.properties",
							"classpath:/intg/config.properties",
							"classpath:/intg/jpush.properties"
							})
	static class IntgConfiguration {}
	
	@Configuration
	@Profile(UserProfile.PROD)
	@PropertySource(value = {
							"classpath:/prod/rpc.properties",
							"classpath:/prod/jdbc.properties",
							"classpath:/prod/config.properties",
							"classpath:/intg/jpush.properties"
							})
	static class ProdConfiguration {}
	
	@Autowired
	Environment evn;
	
	@Bean
	public ConfigUtil configUtil(){
		ConfigUtil util = new ConfigUtil();
		util.setEnv(evn);
		return util;
	}
	
	@Bean(name="config")
	public ConfigProperties config(){
		ConfigProperties config = new ConfigProperties();
		return config;
	}
	
	@Bean(name="jdbcProperties")
	public JdbcProperties jdbcProperties(){
		JdbcProperties config = new JdbcProperties();
		return config;
	}
	
	@Bean(name="jpushProperties")
	public JpushProperties jpushProperties(){
		JpushProperties config = new JpushProperties();
		return config;
	}
	
	@Bean(name="rpcProperties")
	public RpcProperties rpcProperties(){
		RpcProperties config = new RpcProperties();
		return config;
	}
	
	@Bean
	public HttpComponentsClientHttpRequestFactory requestFactory(){
		HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
		return requestFactory;
	}
	
	@Bean
	public RestTemplate restTemplate(){
		RestTemplate template = new RestTemplate(requestFactory());
//		template.setErrorHandler(new MyErrorHandling());
//		template.getInterceptors().add(new AcceptHeaderHttpRequestInterceptor());
		return template;
	}
}
